<?php

/**
 * 关键词管理logic
 */

namespace Wechat\Logic;

class KeywordsLogic extends BaseLogic {
    
	/**
	 * 后去关键词信息
	 * @param string $keyword 关键词信息
	 * @param int $userid 用户id
	 */
	public function detail($keyword,$userid){
		
		if(empty($keyword)){
		    return array();
		}
		
		$map = array(
				/* 'keywords'=>$keyword, */
				'uid'=>$userid,
		);
		
		$keywords = $this->where($map)->where('"'.$keyword.'" REGEXP keywords')->find();
		//判断匹配模式
		if($keywords['matchingtype']!=1){
			if($keywords['keywords'] != $keyword){
				$keywords = null;
			}
		}
		//判断是不是空的消息
		if(empty($keywords)){
		     return array();
		}
		
		$data = array();
		
		//如果是文本消息
		if($keywords['msgtype'] == 'text'){
		    $textmsgLogic = D('Textmsg','Logic');
		    $textmsg = $textmsgLogic->detail($keywords['msgid']);
		   // dump($textmsg);
		    $data[] = $textmsg['text'];
		    $data[] = 'text';
		    
		    return $data;
		}
// 		$keywords['msgtype'] = 'news';
// 		$keywords['msgid'] = '6';
		//如果回复的是图文消息
// 		if( $keywords['msgid'] > 0){
// 			$map = array(
// 			    'id'=>$keywords['msgid'],
// 			);
// 		}else {
// 			$map = array(
// 					'relation'=>$keywords['msgtype'],
// 					'relationid'=>0,
// 			);
// 		}
		if($keywords['msgtype']=='reserve'){
			$newsmsgLogic = D('Newsmsg','Logic');
			$uid = WID;
			//查询所有的预约信息
			$map = array(
					'uid'	=>	$uid,
					'msgtype' => 'reserve_list'
			);
			$lists = $this->where($map)->select();
			foreach ($lists as $key => $value){
					
				$msg = $newsmsgLogic->where(array('id'=>$value['msgid']))->find();
				$msgconfig = serializeMysql($msg['configs'],1);
				$list[$key]['Url'] = C('WEB_ABSOLUTE_URL').'/wechat.php?s=reserve/index/rid/'.$msgconfig['rid'];
				if ($msgconfig['ImgPath']!= null){
					$list[$key]['ImgPath'] = $msgconfig['ImgPath'];
				}else {
					$list[$key]['ImgPath'] = 'http://weixin.jucheng01.net/Public/Member/images/reserve.jpg';
				}
				$list[$key]['Title'] = $msgconfig['Title'];
					
			}
			$data[] =$list;
			$data[] = 'news';
			logs($data);
			return $data;
		}
		if($keywords['msgtype']=='vote'){
			$newsmsgLogic = D('Newsmsg','Logic');
			$uid = WID;
			//查询所有的预约信息
			$map = array(
					'uid'	=>	$uid,
					'msgtype' => 'vote_list'
			);
			$lists = $this->where($map)->select();
			foreach ($lists as $key => $value){
					
				$msg = $newsmsgLogic->where(array('id'=>$value['msgid']))->find();
				$msgconfig = serializeMysql($msg['configs'],1);
				$list[$key]['Url'] = C('WEB_ABSOLUTE_URL').'/wechat.php?s=vote/index/id/'.$msgconfig['vid'];
				if ($msgconfig['ImgPath']!= null){
					$list[$key]['ImgPath'] = $msgconfig['ImgPath'];
				}else {
					$list[$key]['ImgPath'] = 'http://weixin.jucheng01.net/Public/Member/images/vote.jpg';
				}
				$list[$key]['Title'] = $msgconfig['Title'];
					
			}
			$data[] =$list;
			$data[] = 'news';
			logs($data);
			return $data;
		}
		if( in_array($keywords['msgtype'], array('news','onenews','reserve_list'))){
			$map = array(
					'id'=>$keywords['msgid'],
			);
		}else {
				
			if($keywords['msgtype'] == 'businesscard' || $keywords['msgtype']  == 'vips'){
				$keywords['msgtype'] = 'clubcard';
			}
			$map = array(
					'relation'=>$keywords['msgtype'],
					'relationid'=>$keywords['relationid'],
			);
		}
		
		$newsmsgLogic = D('Newsmsg','Logic');
			
		$newsmsg = $newsmsgLogic->detail($map);
// 		logs($newsmsgLogic->getLastSql());
		//设置url
// 		foreach ($newsmsg['configs'] as &$v){
// 		    $v['url'] = api($v['url']);
// // 		    if(strpos($v['ImgPath'], 'http')){
// // 		    	$v['ImgPath'] = C('WEB_ABSOLUTE_URL').$v['ImgPath'];
// // 		    }
		    	
// 		}
			
		if($newsmsg['msgtype'] == 'onenews'){
			$data[][] = $newsmsg['configs'];
		}else{
			$data[] = $newsmsg['configs'];
		}
		
		$data[] = 'news';
		
		return $data;
		 
	}
	
	/* (non-PHPdoc)
	 * @see \Wechat\Logic\BaseLogic::autoSave()
	 */
	public function autoSave($id = 0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-PHPdoc)
	 * @see \Wechat\Logic\BaseLogic::update()
	 */
	public function update($id = 0) {
		// TODO Auto-generated method stub
		
	}

	
	/**
	 * 返回预约列表
	 * @date: 2014-12-9
	 * @author: 明
	 * @return:
	 */
	public function replayReserve(){
		$newsmsgLogic = D('Newsmsg','Logic');
		$uid = WID;
		//查询所有的预约信息
		$map = array(
				'uid'	=>	$uid,
				'msgtype' => 'reserve_list'
		);
		$lists = $this->where($map)->select();
		foreach ($lists as $key => $value){
				
			$msg = $newsmsgLogic->where(array('id'=>$value['msgid']))->find();
			$msgconfig = serializeMysql($msg,1);
			$list[$key]['Url'] = $msgconfig['Url'];
			if ($msgconfig['ImgPath']!= null){
				$lists[$key]['ImgPath'] = $msgconfig['ImgPath'];
			}else {
				$lists[$key]['ImgPath'] = 'http://weixin.jucheng01.net/Public/Member/images/reserve.jpg';
			}
			$list[$key]['Title'] = $msgconfig['Title'];
				
		}
		$data[] =$list;
		$data[] = 'news';
		logs($data);
		return $data;
	}
	
	
	
}